﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace AgencyManagerSystem.Controllers
{
    public class DepartmentController : MyControllerBase
    {
        DepartmentManager manager;
        DepartmentFetcher fetcher;
        public DepartmentController() : base()
        {
            this.manager = new DepartmentManager();
            this.fetcher = new DepartmentFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center,AppOpt.Create)]
        public AsmObject<DepartmentModel> Set(DepartmentModel departmentModel)
        {
            if (departmentModel == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<DepartmentModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(departmentModel), RequestIp));
            var result = manager.Set(departmentModel);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit(true);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmObject<bool> Del(DepartmentModel departmentModel)
        {
            if (departmentModel == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(departmentModel), RequestIp));
            var result = manager.Delete(departmentModel);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit(true);
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DepartmentModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var departmentModels = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit(true);
            var result = new AsmArray<DepartmentModel>() { Data = departmentModels, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<DepartmentModel> Get(int did)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", Util.ConvertToJson(did), RequestIp));
            var department = fetcher.Get(did);
            DBHelper.Commit(true);
            AsmObject<DepartmentModel> result = null;
            if (department != null)
                result = new AsmObject<DepartmentModel>() { Data = department, Message = "操作成功", Result = AmsCode.Succ };
            else
                result = new AsmObject<DepartmentModel>() { Data = department, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Exec)]
        public AsmArray<DepartmentModel> ListByParent(int parent)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYPARENT:[RQS|{1}]{0}", parent, RequestIp));
            var list = fetcher.ListByParent(parent);
            DBHelper.Commit(true);
            var result = new AsmArray<DepartmentModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYPARENT:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DepartmentModel> SearchByName(string name)
        {
            LogHelper.Debug(this.GetType(), String.Format("SEARCHBYNAME:[RQS|{1}]{0}", name, RequestIp));
            var list = fetcher.SearchByName(name);
            DBHelper.Commit(true);
            var result = new AsmArray<DepartmentModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("SEARCHBYNAME:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmArray<Int32> BulkDel(dynamic jsonDepartmentModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RQS|{1}]{0}", jsonDepartmentModel, RequestIp));
            var models = Util.ConvertArrayFromJson<DepartmentModel>((String)jsonDepartmentModel.data);
            if (models == null)
            {
                LogHelper.Err(this.GetType(), String.Format("BULKDEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<Int32>() { Data = new List<Int32>(), Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            int faild = models.Count();
            var del = new List<Int32>();
            StringBuilder sb = new StringBuilder();
            foreach (var model in models)
            {
                var eff = manager.Delete(model);
                if (eff.Result != 0)
                {
                    sb.AppendFormat("{0}:{1}\r\n", model.AutoId, eff.Message);
                }
                else
                {
                    del.Add(model.AutoId);
                    faild--;
                }
            }
            DBHelper.Commit(true);
            var succResponse = new AsmArray<Int32>() { Data = del, Message = "共有" + (models.Count() - faild) + "笔资料删除成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(succResponse), RequestIp));
            return succResponse;
        }
    }
}